<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="600px"
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      label-position="left"
      label-width="120px"
      :model="form"
      :rules="rules"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库编码" label-position="left" prop="whCode">
            <el-input v-model.trim="form.whCode" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="仓库名称" prop="whName">
            <el-input v-model.trim="form.whName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="是否现场仓">
            <el-checkbox v-model.trim="form.sceneFlag" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="是否废品库">
            <el-checkbox v-model.trim="form.gubFlag" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="是否货物管理">
            <el-checkbox v-model.trim="form.cslag" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="是否发货仓">
            <el-checkbox v-model.trim="form.isDelivery" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
  import { ref } from 'vue'
  import { addWarehouseApi, updateWarehouseApi } from '@/api/base/wh'
  import { localStorageConfig } from '@/config/localstorage.config'

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const $baseMessage = inject('$baseMessage')
  const dialogVisible = ref(false),
    dialogTitle = ref('增加仓库'),
    formRef = ref(),
    form = reactive({}),
    rules = reactive({
      whCode: [{ required: true, trigger: 'blur', message: '请输入仓库编码' }],
      whName: [{ required: true, trigger: 'blur', message: '请输入仓库名称' }],
    })

  let baseRecord = ref(null)

  const openDialog = (record) => {
    console.log(record)
    dialogVisible.value = true
    if (record) {
      dialogTitle.value = '修改仓库'
      baseRecord.value = record
      form['pkWh'] = record.pkWh ? record.pkWh : ''
      form['whCode'] = record.whCode ? record.whCode : ''
      form['whName'] = record.whName ? record.whName : ''
      form['sceneFlag'] = record.sceneFlag ? record.sceneFlag : ''
      form['gubFlag'] = record.gubFlag ? record.gubFlag : ''
      form['cslag'] = record.cslag ? record.cslag : ''
      form['isDelivery'] = record.isDelivery ? record.isDelivery : ''
    } else {
      dialogTitle.value = '增加仓库'
      form['whCode'] = ''
      form['whName'] = ''
      form['pkCorp'] = pkCorp
      form['sceneFlag'] = ''
      form['gubFlag'] = ''
      form['cslag'] = ''
      form['isDelivery'] = ''
    }
  }
  const save = () => {
    if (!formRef.value) return
    formRef.value.validate(async (valid) => {
      if (valid) {
        let res = null
        if (baseRecord.value) {
          res = await updateWarehouseApi({
            pkWh: baseRecord.value.pkWh,
            ...form,
          })
        } else {
          res = await addWarehouseApi({
            ...form,
          })
        }
        if (res && res.code === 2000) {
          $baseMessage('操作成功', 'success')
          _emit('success-callback')
          closeDialog()
        } else {
          $baseMessage(res && res.msg ? res.msg : '操作失败,请重试', 'error')
        }
      } else {
        return false
      }
    })
  }
  const closeDialog = () => {
    baseRecord.value = null
    dialogVisible.value = false
  }
  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
